using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace DD.Controls
{
    public partial class login : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            SetDisplay();
        }

        private void SetDisplay()
        {
            DD.Visitors.Visitor vis = new DD.Visitors.Visitor();
            if (vis.IsLoggedIn())
            {
                txt_PASSWORD.Visible = false;
                txt_USERNAME.Visible = false;
                btn_LOGIN.Visible = false;
                lnk_LOGOUT.Visible = true;
            }
            else
            {
                txt_PASSWORD.Visible = true;
                txt_USERNAME.Visible = true;
                btn_LOGIN.Visible = true;
                lnk_LOGOUT.Visible = false;
            }
        }
        protected void btn_LOGIN_Click(object sender, EventArgs e)
        {
            string username = txt_USERNAME.Text;
            string password = txt_PASSWORD.Text;

            Visitors.Visitor vis = new DD.Visitors.Visitor();
            bool login = vis.LoginUser(username, password);
            if (login)
            {
                DD.ShoppingCart.CartManager.AddTempToPermenant();
            }
            else
            {
                DD.Logging.Log.WriteLine("USER " + username + " failed login");
            }


            SetDisplay();
            
        }

        protected void lnk_LOGOUT_Click(object sender, EventArgs e)
        {
            Visitors.Visitor vis = new Visitors.Visitor();
            vis.LogoutUser();
            SetDisplay();
        }

    }
}